package edu.uow.scsse.main.transformation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Read the file
 * @author Tiancheng Zhang
 */

public class ASLFileReader {
	
	private static final Logger logger = Logger.getLogger(ASLFileReader.class);
	
	/**file path*/
	private String filePath;
	/**file content*/
	private List<String> fileContent;
	
	
	
	/**
	 *Constructor for File reader 
	 */
	public ASLFileReader()
	{
		filePath = new String();
		fileContent = new ArrayList<String>();
	}
	
	/**
	 *Constructor for File reader
	 *@param filePath
	 */
	public ASLFileReader(String filePath)
	{
		this.filePath = filePath;
		fileContent = new ArrayList<String>();
	}
	
	/**
	 * @return the filePath
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * @param filePath the filePath to set
	 */
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	/**
	 * @return the fileContent
	 */
	public List<String> getFileContent() {
		return fileContent;
	}

	/**
	 * @param fileContent the fileContent to set
	 */
	public void setFileContent(List<String> fileContent) {
		this.fileContent = fileContent;
	}
	

	/**
	 * Read the file from disk and store into fileContent
	 */
	public void readFile() throws Exception
	{
		logger.info("--------------------START READ FILE---------------------");
		File aslFile = new File(filePath);
		if(!aslFile.exists())
		{
			throw new FileNotFoundException(filePath + " is not found");
		}

		BufferedReader reader = null;
		
		reader = new BufferedReader(new FileReader(aslFile));
		String tempLine = null;
		String reservedLine = "";
		boolean commentFlag = false;
		String element = new String();
		int line = 0;
		while((tempLine = reader.readLine()) != null)
		{
			line++;
			tempLine = tempLine.trim();
			//if it is // comment
			if(tempLine.indexOf("//") != -1)
			{
				tempLine = tempLine.substring(0, tempLine.indexOf("//"));
			}
			//if it is /* comment
			else if(tempLine.indexOf("/*") != -1)
			{
				commentFlag = true;
				//close comment */ in the same line
				if(tempLine.indexOf("*/") != -1)
				{

					tempLine = tempLine.substring(0, tempLine.indexOf("/*"))
							   .concat(((tempLine.indexOf("*/") + 2) == tempLine.length())? "" : (tempLine.substring(tempLine.indexOf("*/") + 2, tempLine.length())));
					commentFlag = false;
				}
				//close comment */ not in the same line
				else
				{
					reservedLine = tempLine.substring(0, tempLine.indexOf("/*"));
				}
			}
			//close */ found, concat with the reservedLine
			else if(tempLine.indexOf("*/") != -1)
			{
				tempLine = reservedLine.concat(tempLine.substring(tempLine.indexOf("*/") + 2, tempLine.length()));
				commentFlag = false;
				//reset reservedLine
				reservedLine = "";
			}
			
			//flush into fileContent
			if(tempLine.length() != 0 && !commentFlag)
			{
				tempLine = tempLine.replaceAll("\t", "").replace(" ", "");
				for(int i = 0; i < tempLine.length(); i++)
				{
					char eachChar = tempLine.charAt(i);
					//if it is action separtor
					if(eachChar == ';')
					{
						pushIntoFileContent(element);
						//reset element to action
						element = "<-";
					}
					//if it is variable separator
					else if(eachChar == ',')
					{
						//do nothing
					}
					//if start of context condition
					else if(eachChar == ':')
					{
						//reset element to context condition
						pushIntoFileContent(element);
						element = ":";
					}
					//if start of action <-
					else if(eachChar == '<' && i+1 < tempLine.length() && tempLine.charAt(i+1) == '-')
					{
						//reset element to action
						pushIntoFileContent(element);
						element = "<-";
						//skip next char
						i++;
						
					}
					else if(eachChar == '+')
					{
						element = "+";
					}
					//the end of a plan
					else if(eachChar == '.' && (i+1 >= tempLine.length() || tempLine.charAt(i+1) == '+'))
					{
						pushIntoFileContent(element);
						if(i+1 >= tempLine.length())
							element = "";
						else if(tempLine.charAt(i+1) == '+')
						{
							element = "+";
						}
					}
					//if it is normal characters
					else
					{
						//add to element
						element += eachChar;
					}
				}
			}

		}
		
		//close the reader
		reader.close();
		logger.info("--------------------END OF READ FILE---------------------");
	}
	
	/**
	 * push an element in to file content list
	 * @param element
	 */
	private void pushIntoFileContent(String element)
	{
		if(element.contains(".print") || element.contains("<--") || element.charAt(0) == '!')
		{
			element = "";
		}
		if(element != null && element.trim().length() != 0)
		{
//			if(element.indexOf(-1))
			element = element.substring(0, element.indexOf('(')==-1? element.length(): element.indexOf('('));
			logger.info(element);
			this.fileContent.add(element);
		}
	}
}
